package com.deyuanyun.pic.integalz.service.impl;

import com.deyuanyun.pic.integalz.controller.dto.HcaRiskDTO;
import com.deyuanyun.pic.integalz.controller.dto.IntegalzQueryVO;
import com.deyuanyun.pic.integalz.mapper.HcaRiskAssessmentMapper;
import com.deyuanyun.pic.integalz.service.HcaRiskAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  完整性分析---高后果区、风险评价统计service实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-11-16
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class HcaRiskAssessmentServiceImpl implements HcaRiskAssessmentService {

    @Autowired
    private HcaRiskAssessmentMapper hcaRiskAssessmentMapper;

    @Override
    public List<Object[]> queryRiskAssessment(IntegalzQueryVO queryVO) {
        List<HcaRiskDTO> hcaRiskDTOS = hcaRiskAssessmentMapper.queryRiskAssessment(queryVO);
        return getResult(queryVO.getEndMileage(), hcaRiskDTOS);
    }

    private List<Object[]> getResult(BigDecimal endMileage, List<HcaRiskDTO> resultList) {
        List<Object[]> resultList2 = new ArrayList<Object[]>();
        if(resultList != null && !resultList.isEmpty()) {
            for (int i = 0, len = resultList.size(); i < len; i++) {
                HcaRiskDTO hcaRiskDTO = resultList.get(i);
                BigDecimal toMileage = hcaRiskDTO.getToMileage();
                BigDecimal toMileage2 = toMileage.setScale(0, BigDecimal.ROUND_DOWN); //向下取整数
                if(endMileage != null && toMileage2.compareTo(endMileage) > 0) {//查询到的的终点距离大于用户输入的终点距离的时候不返回数据
                    continue;
                }
                Object[] strs = new Object[]{hcaRiskDTO.getFromMileage(), toMileage, hcaRiskDTO.getY()};
                resultList2.add(strs);
            }
        }
        return resultList2;
    }

    @Override
    public List<Object[]> queryHca(IntegalzQueryVO queryVO) {
        List<HcaRiskDTO> hcaRiskDTOS = hcaRiskAssessmentMapper.queryHca(queryVO);
        return getResult(queryVO.getEndMileage(), hcaRiskDTOS);
    }

}
